package com.cloud.wms.tools;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.cloud.core.multistate.NavIconType;
import com.cloud.widget.utils.DensityUtils;
import com.cloud.wms.R;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.ui.main.entity.MemberEntity;
import com.dylanc.loadinghelper.LoadingHelper;

import org.jetbrains.annotations.NotNull;

public class BarAdapter extends LoadingHelper.Adapter<BarAdapter.ViewHolder> {

    private String title, rightText;
    private NavIconType type;
    private View.OnClickListener onClickListener;
    private boolean isShow;

    public BarAdapter(String title, NavIconType type) {
        this.title = title;
        this.type = type;
    }

    public BarAdapter(String title, NavIconType type, boolean isShowWareHouse, View.OnClickListener onClickListener) {
        this.title = title;
        this.type = type;
        this.onClickListener = onClickListener;
        this.isShow = isShowWareHouse;
    }

    public BarAdapter(String title, NavIconType type, boolean isShowWareHouse, String rightText, View.OnClickListener onClickListener) {
        this.title = title;
        this.type = type;
        this.onClickListener = onClickListener;
        this.isShow = isShowWareHouse;
        this.rightText = rightText;
    }

    public BarAdapter(String title, NavIconType type, boolean isShow) {
        this.title = title;
        this.type = type;
        this.isShow = isShow;
    }

    @NotNull
    @Override
    public BarAdapter.ViewHolder onCreateViewHolder(@NotNull LayoutInflater inflater, @NotNull ViewGroup parent) {
        return new BarAdapter.ViewHolder(inflater.inflate(R.layout.layout_cloud_toolbar, parent, false));
    }

    @Override
    public void onBindViewHolder(@NotNull BarAdapter.ViewHolder holder) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {       //5.0以上(包含)
//            holder.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            holder.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            holder.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN);
//            holder.getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
//        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {    //4.3以上(不包含)
//            holder.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        }
//
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
//            holder.state.setBackgroundResource(R.color.bg_bar);
//            ViewGroup.LayoutParams lp = holder.state.getLayoutParams();
//            lp.height = DensityUtils.getStatusBarHeight(holder.getActivity());
//            holder.state.setLayoutParams(lp);
//            holder.state.setVisibility(View.VISIBLE);
//        }

        if (!TextUtils.isEmpty(title)) {
            holder.tvTitle.setText(title);
        }

        if (type == NavIconType.BACK) {
            holder.toolbar.setNavigationIcon(R.mipmap.icon_back);
            holder.toolbar.setNavigationOnClickListener(v -> holder.getActivity().finish());
        } else {
            holder.toolbar.setNavigationIcon(null);
        }

        holder.tvWarehouse.setText(FineExApplication.component().loginCache().getWarehouseInfo().getWareHouseName());
        MemberEntity memberInfo = FineExApplication.component().loginCache().getMemberInfo();
        if (memberInfo == null) {
            holder.tvMember.setText("全部");
        } else {
            holder.tvMember.setText(memberInfo.getMemberName());
        }
        if (onClickListener != null)
            holder.llBaseInfo.setOnClickListener(onClickListener);

        if (!isShow) {
            holder.tvWarehouse.setVisibility(View.GONE);
            holder.tvMember.setVisibility(View.GONE);
        }
        if (!isShow && !TextUtils.isEmpty(rightText)) {
            holder.tvRight.setText(rightText);
            holder.tvRight.setVisibility(View.VISIBLE);
        }
        holder.toolbar.setFocusable(false);
    }

    static class ViewHolder extends LoadingHelper.ViewHolder {
        private final Toolbar toolbar;
        private final View llBaseInfo;
        private final TextView tvTitle;
        private final TextView tvWarehouse;
        private final TextView tvMember;
        private final TextView tvRight;
        private final View state;

        ViewHolder(@NonNull View rootView) {
            super(rootView);
            toolbar = rootView.findViewById(R.id.toolbar);
            llBaseInfo = rootView.findViewById(R.id.ll_base_info);
            tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
            tvWarehouse = (TextView) rootView.findViewById(R.id.tv_warehouse);
            tvMember = (TextView) rootView.findViewById(R.id.tv_member);
            tvRight = (TextView) rootView.findViewById(R.id.tv_right);
            state = rootView.findViewById(R.id.id_view_state);
        }

        private Activity getActivity() {
            Context context = getRootView().getContext();
            return (Activity) getRootView().getContext();
        }
    }
}
